package com.swpu.demo.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.swpu.demo.entity.SysUser;
import com.swpu.demo.service.SysUserService;
import com.swpu.demo.utils.JWTUtil;
import com.swpu.demo.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Author Name
 * @since 2021-05-12
 */
@RestController
@RequestMapping("/sysUser")
@CrossOrigin
@Slf4j
@Api("系统管理员交互数据")
public class SysUserController {
    @Autowired
    private SysUserService SysService;
    @GetMapping("/login")
    public Map<String,Object> login(SysUser sysUser, HttpSession session){
        Map<String, Object> map=new HashMap<>();
        SysUser User = SysService.selectOneUser(sysUser.getUserName());
        if(User!=null && User.getPassWord().equals(MD5Util.code(sysUser.getPassWord()))){
            Map<String,String> payload=new HashMap<>();
            payload.put("backId",User.getBackId().toString());
            payload.put("userName", User.getUserName());
            String token = JWTUtil.getToken(payload);
            map.put("msg","登陆成功!");
            map.put("token",token);
            session.setAttribute("sysUser", User);
        }else{
            map.put("msg","账户或密码错误!");
        }
        return map;
    }
    @PostMapping("/update")
    @ApiOperation(value ="更改密码")
    public Map<String, Object> update(HttpServletRequest request,SysUser user){
        //获取请求信息
        String token = request.getHeader("token");
        DecodedJWT verify = JWTUtil.verify(token);
        //log.info("用户id:[{}]",verify.getClaim("backId"));
        //log.info("username:[{}]",verify.getClaim("userName"));
        user.setBackId(Integer.valueOf(verify.getClaim("backId").asString()));
        user.setUserName(verify.getClaim("userName").asString());
        int i = SysService.updateOneUser(user);
        Map<String, Object> map=new HashMap<>();
//        //处理自己业务逻辑
        if(i==1){
            map.put("msg","更新成功！");
        }else {
            map.put("msg","更新失败！");
        }
        return map;
    }
    @PostMapping("/delete")
    @ApiOperation("删除前台用户")
    public Map<String, Object> delete(HttpServletRequest request,SysUser user){
        Map<String, Object> map=new HashMap<>();
        String token = request.getHeader("token");
        JWTUtil.verify(token);
        int i = SysService.deleteOneUser(user.getBackId());
        if(i==1){
            map.put("msg","更新成功！");
        }else {
            map.put("msg","更新失败！");
        }
        return map;
    }
    @RequestMapping("/logout")
    @ApiOperation("退出登录")
    public Map<String, Object> logOut(HttpServletRequest request, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        session.removeAttribute("sysUser");
        map.put("msg", "退出成功");
        return map;
    }
}

